//
//  Permissions.swift
//  EyesCare
//
//  Created by PMC on 2018/11/30.
//  Copyright © 2018年 qdsg. All rights reserved.
//

import UIKit
import Photos

struct Permissions {
    
    /// 授权相机权限
    static func authorizeCamera(completion: @escaping (_ granted: Bool) -> ()) {
        let granted = AVCaptureDevice.authorizationStatus(for: .video)
        switch granted {
        case .authorized:
            completion(true)
        case .restricted, .denied:
            completion(false)
        case .notDetermined:
            AVCaptureDevice.requestAccess(for: .video) { (isGranted) in
                completion(isGranted)
            }
        @unknown default: break
        }
    }
    
    /// 授权相册权限
    static func authorizeAlbum(completion: @escaping (_ granted: Bool) -> ()) {
        let granted = PHPhotoLibrary.authorizationStatus()
        switch granted {
        case .authorized:
            completion(true)
        case .restricted, .denied:
            completion(false)
        case .notDetermined:
            PHPhotoLibrary.requestAuthorization { (status) in
                completion(status == .authorized)
            }
        default: break
        }
    }
}
